You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups.  

You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.  

**SPECIAL INSTRUCTIONS FOR ATOMIC-INTENSIVE OPERATORS:**

When the target operator involves reduction operations with atomic operations (like loss functions that sum across batch dimension), you MUST implement the following optimization strategy:

1. **DUAL KERNEL APPROACH**: Implement two CUDA kernels:
   - **Smart Kernel**: Uses dynamic block allocation based on batch size to reduce atomic contention
   - **Efficient Kernel**: Uses two-level reduction (shared memory + partial sums) to minimize atomic operations

2. **SMART BLOCK ALLOCATION STRATEGY**:
   cpp
// Implement this exact logic in your smart kernel:
int num_blocks;
if (batch_size <= 64) num_blocks = 1;
else if (batch_size <= 128) num_blocks = 1;
else if (batch_size <= 256) num_blocks = 2; // Critical: increase blocks here
else if (batch_size <= 512) num_blocks = 4;
else num_blocks = min(16, (batch_size + 255) / 256);

3. **EFFICIENT REDUCTION KERNEL**:
   - Use shared memory for block-level reduction
   - Each thread processes multiple samples with stride
   - Store partial sums in global array, then final reduction
   - Use `extern __shared__ float shared_mem[]` for reduction

4. **MATHEMATICAL PRECISION**: Ensure exact mathematical alignment:
   - Use `sqrtf(distance_sq)` for actual distance (not squared distance)
   - Match PyTorch's pairwise_distance behavior exactly
   - Implement standard contrastive loss: `0.5 * [y * d² + (1-y) * max(0, margin - d)²]`

5. **MODE SELECTION**: Implement mode parameter to choose between strategies:
   - `mode="smart"`: Use dynamic block allocation
   - `mode="efficient"`: Use two-level reduction
   - Default to smart mode for general use

6. **OPTIMIZATION TECHNIQUES**:
   - Loop unrolling for fixed dimensions
   - Register optimization with `register` keyword
   - Fused computation (diff + square + sum in one loop)
   - Use `fmaxf(0.0f, margin_diff)` instead of branching

Here's the target architecture to optimize:

python
import torch
import torch.nn as nn

class Model(nn.Module):
“”"
Contrastive Loss implementation for learning embeddings.
Computes contrastive loss between anchor and sample embeddings.
“”"
def init(self, margin=2.0):
super(Model, self).init()
self.margin = margin

def forward(self, anchor: torch.Tensor, sample: torch.Tensor, label: torch.Tensor) -> torch.Tensor:  
    """  
    Compute contrastive loss between anchor and sample embeddings.

    Args:  
        anchor (torch.Tensor): Anchor embeddings [batch_size, feature_dim]  
        sample (torch.Tensor): Sample embeddings [batch_size, feature_dim]  
        label (torch.Tensor): Binary labels [batch_size] (0=similar, 1=dissimilar)

    Returns:  
        torch.Tensor: Scalar contrastive loss value
    """  
    # Compute pairwise Euclidean distances
    distances = torch.nn.functional.pairwise_distance(anchor, sample, p=2)
    
    # Apply contrastive loss formula
    losses = 0.5 * (label.float() * distances.pow(2) + 
                    (1 - label).float() * torch.nn.functional.relu(self.margin - distances).pow(2))
    
    return torch.sum(losses)
batch_size = 128
feature_dim = 512
margin = 2.0

def get_inputs():
anchor = torch.randn(batch_size, feature_dim)
sample = torch.randn(batch_size, feature_dim)
label = torch.randint(0, 2, (batch_size,)).float()
return [anchor, sample, label]

def get_init_inputs():
return [margin] # margin parameter



**EXPECTED OUTPUT STRUCTURE**:
Generate two files:
1. `example_cudacode.py` - Contains ModelNew class with dual-kernel approach
2. `example_torchcode.py` - Contains the reference PyTorch implementation

**KEY REQUIREMENTS**:
- The CUDA implementation must handle atomic operation contention for different batch sizes
- Must include both smart and efficient kernel strategies
- Must maintain mathematical precision with PyTorch's pairwise_distance
- Must support mode selection between optimization strategies
- Must use the exact block allocation strategy shown above